#include "core/laurin-discover-network-clients-module.h"
#include "core/laurin-discover-devices-module.h"
#include "core/laurin-utils.h"
#include <glibmm/thread.h>
#include <glibmm/main.h>
#include <iostream>
#include <list>
using namespace std;


Glib::RefPtr<Glib::MainContext> main_context;
Glib::RefPtr<Glib::MainLoop> loop;


bool timeout()
{
	loop->quit();
	return false;
}

int main()
{
	if(!Glib::thread_supported())
		Glib::thread_init();


	LaurinDiscoverNetworkClientsModule* discover = new LaurinDiscoverNetworkClientsModule("wlan0");
	LaurinDiscoverDevicesModule* discover2 = new LaurinDiscoverDevicesModule("mon0", "wmaster0");
	discover2->start();
	discover->start();

	// 10 seconds
	const Glib::RefPtr<Glib::TimeoutSource> timeout_source = Glib::TimeoutSource::create(10000);
	timeout_source->connect(sigc::ptr_fun(&timeout));
	timeout_source->attach(main_context);

	main_context = Glib::MainContext::get_default();
	loop = Glib::MainLoop::create(main_context, false);

	loop->run();

	discover->stop(true);
	discover2->stop(true);

	discover2->elaborateFrames();


	std::list<LaurinNetworkClient>::const_iterator b = discover->getNetwork()->getClients().begin();
	std::list<LaurinNetworkClient>::const_iterator e = discover->getNetwork()->getClients().end();

	while(b != e)
	{
		cout<<"found client with ip: "<<getReadableIP((*b).getIP())<<endl;
		b++;
	}

	cout<<endl;

	std::list<Laurin80211Network>::const_iterator nb = discover2->getNetworkSet()->getNetworks().begin();
	std::list<Laurin80211Network>::const_iterator ne = discover2->getNetworkSet()->getNetworks().end();

	while(nb != ne)
	{
		cout<<endl<<"founded network with ssid: "<<(*nb).getNetworkSSID().c_str()<<endl;
		cout<<"bssid are:"<<endl;

		std::list<LaurinAccessPoint>::const_iterator ab = (*nb).getAccessPoint().begin();
		std::list<LaurinAccessPoint>::const_iterator ae = (*nb).getAccessPoint().end();

		while(ab != ae)
		{
			cout<<"mac address "<<getReadableMAC((*ab).getMAC())<<endl;
			ab++;
		}

		cout<<endl<<"clients are:"<<endl;

		std::list<Laurin80211Station>::const_iterator sb = (*nb).getStations().begin();
		std::list<Laurin80211Station>::const_iterator se = (*nb).getStations().end();

		while(sb != se)
		{
			cout<<"mac address: "<<getReadableMAC((*sb).getMAC())<<endl;
			sb++;
		}


		nb++;
	}



}
